package net.m510.autosurfer;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.logging.Logger;

import net.m510.autosurfer.common.DocumentGetProcess;
import net.m510.autosurfer.common.cli.OutDirOption;
import net.m510.autosurfer.common.cli.OutFileOption;
import net.m510.autosurfer.common.cli.PasswordOption;
import net.m510.autosurfer.core.AbstractMain;
import net.m510.autosurfer.core.UserAgents;
import net.m510.autosurfer.core.exception.UserProcessException;
import net.m510.autosurfer.kddi.KddiAuIdLogin;
import net.m510.autosurfer.kddi.KddiGoAuIdLogin;
import net.m510.autosurfer.sonybank.SonyBankAnnouncement;
import net.m510.autosurfer.sonybank.SonyBankCheckLastDeal;
import net.m510.autosurfer.sonybank.SonyBankDownload;
import net.m510.autosurfer.sonybank.SonyBankGoAccountTop;
import net.m510.autosurfer.sonybank.SonyBankGoDealHistory;
import net.m510.autosurfer.sonybank.SonyBankLogin;
import net.m510.autosurfer.sonybank.SonyBankLoginChecker;
import net.m510.autosurfer.sonybank.SonyBankLogout;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jsoup.nodes.Document;

public class KddiGetMain extends AbstractMain {
	private static final Logger log = Logger.getLogger(KddiGetMain.class.getName());
	private static final String DEFAULT_USER_AGENT = UserAgents.CHROME;
	
	private String auId;
	private String secretNum;
	private String password;
	private File outDir;

	/**
	 * 
	 */
	public KddiGetMain() {
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		KddiGetMain main = new KddiGetMain();
		main.setLogger(log);
		main.Main(args);
	}
	@Override
	protected Document start() throws IOException, UserProcessException {
		// auIDでログインページへ
		KddiGoAuIdLogin p = new KddiGoAuIdLogin(client, context);
		Document out = p.process(null);
		return out;
	}

	@Override
	protected Document login(Document doc) throws IOException, UserProcessException {
		// ログイン
		KddiAuIdLogin p = new KddiAuIdLogin(client, context);
		p.setAuId(auId);
		p.setPassword(password);
		Document out = p.process(doc);
		log.fine("login src="+out.html());
//		new SonyBankLoginChecker().check(out);
		// user-agent:だめ
		// referer:だめ
		// cookie:だめ
		// toppage　URL変更：だめ
		return out;
	}

	@Override
	protected Document prepareDownload(Document doc) throws IOException,UserProcessException {
		// 請求書・領収書のダウンロード
		// 請求書のダウンロード
		// 暗証番号入力
		return null;
//		log.info("前回取引確認処理開始");
//		Document out1 = new SonyBankCheckLastDeal(client, context).process(doc);
//		log.info("前回取引確認処理終了 ");
//		
//		log.info("お知らせ処理開始");
//		Document out2 = new SonyBankAnnouncement(client, context).process(out1);
//		log.info("お知らせ処理終了 ");
//
//		log.info("口座トップページへの転送処理開始");
//		Document out3 = new SonyBankGoAccountTop(client, context).process(out2);
//		log.info("口座トップページへの転送処理終了 ");
//
//		log.info("取引履歴ページへの遷移開始");
//		Document out4 = new SonyBankGoDealHistory(client, context).process(out3);
//		log.info("取引履歴ページへの遷移終了");
//		return out4;
	}

	@Override
	protected File download(Document doc) throws IOException, UserProcessException {
		// PDFダウンロード
		return null;
//		SonyBankDownload p = new SonyBankDownload(client, context, outFile);
//		File out = p.process(doc);
//		return out;
	}

	@Override
	protected void logout(Document doc) throws IOException,	UserProcessException {
		// au ID ログアウト
//		new SonyBankLogout(client, context).process(doc);
	}
	
	@Override
	protected void customClientBuilder(HttpClientBuilder builder) {
//		builder.setUserAgent(DEFAULT_USER_AGENT);
//		RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
		RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build();
		builder.setDefaultRequestConfig(config);
	}
	
	@SuppressWarnings("static-access")
	@Override
	protected Options getOptions() {
		Options options = new Options();
		options.addOption(new OutDirOption(false));
		options.addOption(OptionBuilder.hasArg(true).withArgName("phone no")
				.isRequired(true).withDescription("au ID")
				.create("auid"));
		options.addOption(OptionBuilder.hasArg(true).withArgName("secret num")
				.isRequired(true).withDescription("暗証番号")
				.create("secnum"));
		options.addOption(new PasswordOption(true));
		return options;
	}

	@Override
	protected void setParams(CommandLine cmd) {
		auId = cmd.getOptionValue("auid");
		secretNum = cmd.getOptionValue("account");
		password = cmd.getOptionValue("password");
		outDir = new File(cmd.getOptionValue(OutDirOption.OPT));
	}

}
